package com.itheima.service.mongo.impl;

import com.itheima.domain.db.User;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bouncycastle.util.Integers;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser findTodayBest(Long toUserId) {
        // 1.构建条件
        Query query = new Query(
                Criteria.where("toUserId").is(toUserId)
        ).with(Sort.by(Sort.Order.desc("score")))
                .skip(0).limit(1);
        // 2.查询推荐用户
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        // 3.返回一条
        return recommendUser;
    }

    // 查询推荐用户列表
    @Override
    public PageBeanVo findRecommedUserByPage(Long toUserId, Integer pageNum, Integer pageSize) {
        // 1.构建条件
        Integer index = (pageNum - 1) * pageSize;
        Query query = new Query(
                Criteria.where("toUserId").is(toUserId) // 条件
        ).with(Sort.by(Sort.Order.desc("score"))) // 排序
                .skip(index + 1).limit(pageSize); // 分页

        // 2.查询list
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        // 3.查询count
        long count = mongoTemplate.count(new Query(
                Criteria.where("toUserId").is(toUserId) // 条件
        ).with(Sort.by(Sort.Order.desc("score"))), RecommendUser.class);
        // 4.封装并返回分页对象
        return new PageBeanVo(pageNum, pageSize, count, list);
    }

    // 查询二个的缘分值
    @Override
    public RecommendUser findByJiarenIdAndToUserId(Long jiarenId, Long toUserId) {
        // 1.构建条件
        Query query = new Query(
                Criteria.where("userId").is(jiarenId)
                        .and("toUserId").is(toUserId)
        );
        // 2.查询并返回结果
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    //查询推荐用户卡片
    @Override
    public List<RecommendUser> findRecommendUser(Long toUserId) {
        //查询用户信息，并将结果返回给集合
        Query query = new Query(
                Criteria.where("toUserId").is(toUserId)
        );
        return  mongoTemplate.find(query, RecommendUser.class);
    }

    //滑动删除
    @Override
    public void remove(Long userId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        mongoTemplate.remove(query, RecommendUser.class);
    }

    //滑动喜欢
    @Override
    public void loveUser(UserLike userLike) {
        //构建条件

        mongoTemplate.save(userLike);
    }

    //判断对方时候喜欢我
    @Override
    public UserLike alreadyLove(Long jiarenId, Long toUserId) {
        Query query = new Query(
                Criteria.where("userId").is(toUserId).and("likeUserId").is(jiarenId)
        );
        return mongoTemplate.findOne(query, UserLike.class);

    }

    //查询，，粉丝
    @Override
    public Long findLoveCount(Long userId) {
        //构建条件
        Query fanQuery = new Query(
                Criteria.where("userId").is(userId)
        );
        long fanCount = mongoTemplate.count(fanQuery, UserLike.class);

        return fanCount;
    }
    //喜欢
    @Override
    public Long findFanCount( Long likeUserId) {
        Query loveQuery = new Query(
                Criteria.where("likeUserId").is(likeUserId)
        );

        long loveCount = mongoTemplate.count(loveQuery, UserLike.class);
        return loveCount;
    }

    //互相喜欢
    @Override
    public Long findEachLoveCount( Long userId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );

        long eachLoveCount = mongoTemplate.count(query, Friend.class);
        return eachLoveCount;
    }

    //根据类型查询所需要的分页数据（查询我喜欢的，我的粉丝，相互喜欢以及看我的人）
    @Override
    public PageBeanVo findFriendsType(String type, Long userId, Integer pageNum, Integer pageSize) {
        switch (type) {
            case "1": {
                //构建条件查询我的好友列表
                Query query = new Query(
                        Criteria.where("userId").is(userId)
                ).skip((pageNum - 1) * pageSize).limit(pageSize);

                List<Friend> list = mongoTemplate.find(query, Friend.class);

                long count = mongoTemplate.count(new Query(
                        Criteria.where("userId").is(userId)
                ), Friend.class);

                return new PageBeanVo(pageNum, pageSize, count, list);

            }
            case "2": {
                Query query = new Query(
                        Criteria.where("userId").is(userId)
                ).skip((pageNum - 1) * pageSize).limit(pageSize);
                List<UserLike> list = mongoTemplate.find(query, UserLike.class);
                long count = mongoTemplate.count(new Query(
                        Criteria.where("userId").is(userId)
                ), UserLike.class);
                return new PageBeanVo(pageNum, pageSize, count, list);
            }
            case "3": {
                Query query = new Query(
                        Criteria.where("likeUserId").is(userId)
                ).skip((pageNum - 1) * pageSize).limit(pageSize);
                List<UserLike> list = mongoTemplate.find(query, UserLike.class);
                long count = mongoTemplate.count(new Query(
                        Criteria.where("likeUserId").is(userId)
                ), UserLike.class);
                return new PageBeanVo(pageNum, pageSize, count, list);
            }
            case "4": {
                Query query = new Query(
                        Criteria.where("userId").is(userId) // 登录人id
                ).with(Sort.by(Sort.Order.desc("date"))); // 时间降序

                List<Visitor> list = mongoTemplate.find(query, Visitor.class);
                long count = mongoTemplate.count(query, Visitor.class);

                return new PageBeanVo(pageNum, pageSize, count, list);
            }

        }
        return null;
    }

}
